<?php
if (!defined('FosfoaSystem')) {
    exit(1);
}
class productsModel extends spModel
{
    public $pk = 'products_id';
    public $table = 'products';
    // 由spModel的变量$linker来设置表间关联
    var $linker = array(array(
            'type' => 'hasone', // 关联类型，这里是一对一关联
            'map' => 'detail', // 关联的标识
            'mapkey' => 'products_id', // 本表与对应表关联的字段名
            'fclass' => 'products_descriptionModel', // 对应表的类名
            'fkey' => 'products_id', // 对应表中关联的字段名
            //'condition'=>'products_name="ttt"',
            'enabled' => true // 启用关联
                ));
    
    function Detail($products_id)
    {
        $condition = array('products_id' => $products_id);
        $arr = $this->spCache(3600 * 24)->find($condition);
        return $arr;
    }
    /**
     * 销售统计
     */
     function Saleadd($products_id)
     {
        $this->incrField(array('products_id'=>$products_id), 'products_ordered');
     }
    /**
     * 浏览统计
     */
     function hits($products_id)
     {
        $this->incrField(array('products_id'=>$products_id), 'hits');
     }
     
    /**
     * 返回所有商品含分页
     * @conditions 条件
     * @currentpage 当前页
     * @pagesize 每页数量
     * @order order 排序 1随机 2最新 3最旧 4价高到底 5价低到高 6销售排行 7人气
     */
    function products_all($conditions, $current_page, $page_size, $order = 2)
    {
        switch ($order) {
            case 1:
                //随机
                $order = 'RANDOM()';
                break;
            case 2:
                //最新
                $order = 'products_id DESC';
                break;
            case 3:
                //最旧
                $order = 'products_id ASC';
                break;
            case 4:
                //价格由高到低
                $order = 'products_price DESC';
                break;
            case 5:
                //价格由低到高
                $order = 'products_price ASC';
                break;
            case 6:
                //销售排行
                $order = 'products_ordered DESC';
                break;
            case 7: 
                //最热
                $order='hits desc';
                break; 
            default:
                $order = 'RANDOM()';
        }

        $fields1 = "products_id,products_image,alias,master_categories_id,products_date_added,products_price_discounted,products_price";
        $fields2 = "products_name,products_title,products_seokeywords,products_description,products_seodescription"; //这里限制被关联表的字段
        //$order = "products_id desc";
        $this->linker[0]['field'] = $fields2;
        $request['productslist'] = $this->spLinker()->spPager($current_page, $page_size)->
            findAll($conditions, $order, $fields1);
        $request['pager'] = $this->spLinker()->spPager()->getPager();
        return $request;
    }


    /**
     * 随机产品
     * @name conditions 随机的条件 $conditions = "a.master_categories_id in(" . $a . ")";
     * @name num 随机的数量 默认为5个
     * @order order 排序 1随机 2最新 3最旧 4价高到底 5价低到高 6销售排行 7人气
     */
    function products_random($conditions, $num = 5, $order = 1)
    {
        switch ($order) {
            case 1:
                //随机
                $order = 'RANDOM()';
                break;
            case 2:
                //最新
                $order = 'products_id DESC';
                break;
            case 3:
                //最旧
                $order = 'products_id ASC';
                break;
            case 4:
                //价格由高到低
                $order = 'products_price DESC';
                break;
            case 5:
                //价格由低到高
                $order = 'products_price ASC';
                break;
            case 6:
                //销售排行
                $order = 'products_ordered DESC';
                break;
            case 7: 
                //最热
                $order='hits DESC';
                break;                
            default:
                $order = 'RANDOM()';
        }

        $fields1 = "products_id,products_image,alias,master_categories_id,products_date_added,products_price_discounted,products_price,hits";
        $fields2 = "products_name,products_title,products_seokeywords,products_description,products_seodescription"; //这里限制被关联表的字段
        $this->linker[0]['field'] = $fields2;
        $request = $this->spLinker()->findAll($conditions, $order, $fields1, $num); // 执行查找
        return $request;
    }

    /**
     * 别名检查
     */
    function aliascheck($alias, $pid)
    {
        $conditions = array('alias' => $alias);
        $request = $this->find($conditions);
        if ($request) {
            if ($request['products_id'] == $pid) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
    /**
     * 根据别名返回信息
     */
    function FindformAlias($alias)
    {
        $conditions = array('alias' => $alias);
        $request = $this->spLinker()->spCache(3600 * 24)->find($conditions);
        if ($request) {
            return $request;
        } else {
            return false;
        }
    }
    /**
     * 根据ID返回信息
     */
    function FindformID($products_id)
    {
        $conditions = array('products_id' => $products_id);
        $request = $this->spLinker()->spCache(3600 * 24)->find($conditions);
        if ($request) {
            return $request;
        } else {
            return false;
        }
    }

    /**
     * 返回集合ID下的新商品
     * @cid 分类ID集合 如 1,2,3
     * @num 显示数量
     * @order 排序 排序 1随机 2最新 3最旧 4价高到底 5价低到高 6销售排行 7人气 
     * @param $type 1全部 2推荐商品
     */
    function New_products_in($cid, $num,$order,$type=1)
    {
        if ($cid) {
            $tmparray = explode(',', $cid);
            if (count($tmparray) > 1) {
                $conditions = 'master_categories_id in(' . $cid . ')';
            } else {
                $conditions = 'master_categories_id =' . $cid;
            }
        }else{
            $conditions = '1=1';
        }
        $conditions .=($type==2)?' and type=1':'';
        if($order<>1){
          return $this->spCache(3600 * 24)->products_random($conditions, $num,$order);  
        }else{
            return $this->products_random($conditions, $num,$order);
        }
    }
    /**
     * 返回某ID下的商品数量
     * @cid 分类ID集合 如 1,2,3
     */
    function Product_count($cid)
    {
        if ($cid) {
            $tmparray = explode(',', $cid);
            if (count($tmparray) > 1) {
                $conditions = 'master_categories_id in(' . $cid . ')';
            } else {
                $conditions = 'master_categories_id =' . $cid;
            }
        }else{
            $conditions = '1=1';
        }
        return $this->findCount($conditions);
    }
    /**
     * 查询上一条
     */
     function Previous($cid,$productsid)
     {
       $fields= "products_name";
       $condition = 'master_categories_id=' . $cid .' and products_id<' . $productsid;
       $this->linker[0]['field'] = $fields;
       $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'products_id desc');
       if (!$request)
       {
         $condition = 'master_categories_id=' . $cid;
         $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'products_id desc');
       }
       return $request;
     }
    /**
     * 查询下一条
     */
     function Next($cid,$productsid)
     {
       $fields= "products_name";
       $condition = 'master_categories_id=' . $cid .' and products_id>' . $productsid;
       $this->linker[0]['field'] = $fields;
       $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'products_id asc');
       if (!$request)
       {
         $condition = 'master_categories_id=' . $cid;
         $request = $this->spLinker()->spCache(3600 * 24)->find($condition, 'products_id asc');
       }
       return $request;
     }
}
